package net.phys2d.raw.test;

import java.awt.Toolkit;
import java.awt.event.KeyEvent;

import net.phys2d.raw.*;
import net.phys2d.raw.shapes.*;
import net.phys2d.math.*;

public class DemoMario extends AbstractDemo {
	private Body leg1;
	private Body leg2;
	private Joint joint;
	private boolean left = false;
	private boolean right = false;

	public DemoMario(){
		super("Mario");
	}
	
	protected void keyPress(int keycode){
		super.keyPress(keycode);
		if(keycode == KeyEvent.VK_SPACE){
			leg1.setTorque(300000);
		}else if(keycode == KeyEvent.VK_RIGHT){
			if(!left){
				right = true;
			}
		}else if(keycode == KeyEvent.VK_LEFT){
			if(!right){
				left = true;
			}
		}
	}
	
	protected void keyRelease(int keycode){
		if(keycode == KeyEvent.VK_RIGHT){
			right = false;
		}else if(keycode == KeyEvent.VK_LEFT){
			left = false;
		}
	}
	
	protected void update(){
		super.update();
		if(right){
			leg1.adjustVelocity(new Vector2f(10, 0));
		}else if(left){
			leg1.adjustVelocity(new Vector2f(-10, 0));
		}
		leg1.setTorque(0);
		//leg1.setRotation(0f);
	}
	
	@Override
	protected void init(World world) {
		world.setGravity(0, 20);
		Body ground = new StaticBody("Wall 1", new Box(20, 500));
		ground.setPosition(10, 250);
		world.add(ground);
		ground = new StaticBody("Ground", new Box(1000, 20));
		ground.setPosition(520, 490);
		world.add(ground);
		ground = new StaticBody("Ceiling", new Box(1000, 20));
		ground.setPosition(520, 100);
		ground.setFriction(10000);
		world.add(ground);
		ground = new StaticBody("Wall 2", new Box(20, 500));
		ground.setPosition(1030, 250);
		world.add(ground);
		ROVector2f[] vecs = {new Vector2f(0,0), new Vector2f(0, -40), new Vector2f(10, -50), new Vector2f(20, -40), new Vector2f(20, 0)};
		//leg1 = new Body("Leg1", new Box(15,40), 10);
		leg1 = new Body("Leg1", new Polygon(vecs), 10);
		leg2 = new Body("Leg2", new Box(15,40), 10);
		
		leg2.setDamping(.1f);
		leg2.setPosition(150, 300);
		leg2.setFriction(1);
		leg2.setMaxVelocity(200,200);
		world.add(leg2);
		
		leg1.setDamping(.1f);
		leg1.setPosition(100, 300);
		leg1.setFriction(1);
		leg1.setMaxVelocity(200,200);
		world.add(leg1);
		
		joint = new BasicJoint(leg1, leg2, new Vector2f(leg1.getPosition().getX()+100,leg1.getPosition().getY()-100));
		world.add(joint);
	}

	public static void main(String[] argv) {
		DemoMario demo = new DemoMario();
		demo.start();
	}
}
